package com.g9.blog.service.impl;

import com.g9.blog.entity.Article;
import com.g9.blog.entity.Comment;
import com.g9.blog.repository.ArticleRepository;
import com.g9.blog.repository.CommentRepository;
import com.g9.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: Graycat
 * @Date: 2020/9/12 09:06
 * Create by lwj.
 */

@Service
@Transactional
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentRepository commentRepository;
    @Autowired
    ArticleRepository articleRepository;

    @Override
    public List<Comment> getCommentByArticleId(int articleId) {
        return commentRepository.findAllByArticleId(articleId);
    }

    @Override
    public void addArticleCommentCount( Comment comment ) {
        List<Article> articleList = articleRepository.findAllById(comment.getArticleId());
        articleList.get(0).setCommentCount(articleList.get(0).getCommentCount()+1);
        articleRepository.save(articleList.get(0));
    }

    @Override
    public void addCommentLikeCount(int commentId) {
        Comment comment = commentRepository.findById(commentId);
        comment.setCommentLikeCount(comment.getCommentLikeCount()+1);
        commentRepository.save(comment);
    }

    @Override
    public void save(Comment comment) {
        commentRepository.save(comment);
    }

    @Override
    public void deleteById(int commentId) {
        commentRepository.deleteById(commentId);
    }

    @Override
    public Comment findById(int commentId) {
        return commentRepository.findById(commentId);
    }
}
